package com.einmeer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.einmeer.api.FileService;
import com.einmeer.entity.UmsAdmin;
import com.einmeer.mapper.UmsAdminMapper;
import com.einmeer.service.UmsAdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.time.LocalDate;

/**
 * <p>
 * 管理员表 服务实现类
 * </p>
 *
 * @author einmeer
 * @since 2024-03-18
 */
@Service
public class UmsAdminServiceImpl extends ServiceImpl<UmsAdminMapper, UmsAdmin> implements UmsAdminService {
    @Resource
    FileService fileService;
    @Resource
    PasswordEncoder passwordEncoder;


    @Override
    public IPage<UmsAdmin> list(Integer pageNo, Integer pageSize,String name) {
        QueryWrapper<UmsAdmin> wrapper = new QueryWrapper<>();
        if (StringUtils.isNoneBlank(name)){
            wrapper.like("name",name);
        }
        return this.page(new Page<>(pageNo,pageSize),wrapper);
    }

    @Override
    public boolean add(String name, String phone, String email, LocalDate birthday, String password, MultipartFile file) {
        String icon = fileService.upload(file);
        UmsAdmin umsAdmin = new UmsAdmin(
                name,
                phone,
                email,
                fileService.upload(file),
                birthday,
                passwordEncoder.encode(password),
                true
        );
        return this.save(umsAdmin);
    }

    // 判断字段是否重复
    @Override
    public boolean check(String field, String value) {
        QueryWrapper<UmsAdmin> wrapper = new QueryWrapper<>();
        wrapper.eq(field,value);
        return this.count(wrapper)==0;
    }
}
